package com.huike.controller.adminController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huike.entity.Admin;
import com.huike.entity.Policy;
import com.huike.service.IPolicyService;
import com.huike.utils.JsonUtilsForRichText;
import com.huike.utils.ResultMessage;

/**
 * 政策管理控制器
 */
@Controller
@RequestMapping("/admin/policyManagerController")
public class PolicyManagerController {

	@Autowired
	IPolicyService service;

	@RequestMapping("")
	public String page() {
		return "admin/PolicyManager";
	}

	/**
	 * 解决页面传来时间格式转换失败问题
	 * 
	 * @param binder
	 */
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	/**
	 * 分页显示资讯
	 * 
	 * @param response
	 * @param request
	 * @param page
	 * @param limit
	 */
	@RequestMapping("/listPolicy")
	@ResponseBody
	public void listPolicy(HttpServletResponse response, HttpServletRequest request, int page, int limit) {
		HttpSession session = request.getSession();
		Admin admin = (Admin) session.getAttribute("admin");
		IPage<Policy> res = service.listByPage(admin, page, limit);
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("total", res.getTotal());
		map.put("data", res.getRecords());
		JsonUtilsForRichText.jsonArray(map, response);
	}

	/**
	 * 添加方法
	 * 
	 * @param request
	 * @param information
	 * @return
	 */
	@RequestMapping("/addPolicy")
	@ResponseBody
	public ResultMessage addPolicy(HttpServletRequest request, Policy policy) {
		HttpSession session = request.getSession();
		Admin admin = (Admin) session.getAttribute("admin");
		boolean b = service.insertPolicy(admin, policy);
		if (b)
			return ResultMessage.success();
		return ResultMessage.fail();
	}

	/**
	 * 修改方法
	 * 
	 * @param information
	 * @return
	 */
	@RequestMapping("/updateFunc")
	@ResponseBody
	public ResultMessage updateFunc(Policy policy) {
		boolean b = service.updateById(policy);
		if (b)
			return ResultMessage.success();
		return ResultMessage.fail();
	}

	/**
	 * 删除方法
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/deletePolicy")
	@ResponseBody
	public ResultMessage deletePolicy(@RequestParam(required = false, value = "ids[]") List<String> ids) {
		boolean b = service.deletePolicy(ids);
		if (b)
			return ResultMessage.success();
		return ResultMessage.fail();
	}

}
